package com.example.server.trasaction;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.example.server.annotation.HaeTransaction;
import com.example.server.transport.NettyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.UUID;

/**
 * @Auther: yuanjianpeng
 * @Date: 2019/4/29 11:55
 * @Description:
 */
@Component
public class HaeTransactionManager {

//    @Autowired
    private static NettyClient nettyClient;

    private static HashMap<String, HaeTransactionModel> HAE_TRANSACTION_MAP = new HashMap<>();

    private static ThreadLocal<HaeTransactionModel> current = new ThreadLocal<>(); //存储HaeTransactionModel便于同一线程共享
    private static ThreadLocal<String> currentGroupId = new ThreadLocal<>();
    private static ThreadLocal<Integer> transactionCount = new ThreadLocal<>();

    public static String createHaeTransactionGroup() {
        String groupId = UUID.randomUUID().toString();
        currentGroupId.set(groupId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupId",groupId);
        jsonObject.put("command","create");
        nettyClient.send(jsonObject);
        System.out.println("创建事务组");
        return groupId;
    }

    public static HaeTransactionModel createHaeTransaction(String groupId) {
        String transactionId = UUID.randomUUID().toString();
        HaeTransactionModel haeTransactionModel = new HaeTransactionModel(transactionId, groupId);
        System.out.println("创建事务");
        HAE_TRANSACTION_MAP.put(groupId, haeTransactionModel);
        current.set(haeTransactionModel); //将当前线程的haeTransactionModel存储起来便于共享
        return haeTransactionModel;
    }

    public static HaeTransactionModel addHaeTransactionModel(HaeTransactionModel haeTransactionModel, Boolean isEnd,
                                                             TransactionType transactionType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupId",haeTransactionModel.getGroupId());
        jsonObject.put("transactionId",haeTransactionModel.getTransactionId());
        jsonObject.put("transactionType",transactionType);
        jsonObject.put("command","add");
        jsonObject.put("isEnd","isEnd");
        nettyClient.send(jsonObject);
        System.out.println("添加事务");
        return haeTransactionModel;
    }

    public static HaeTransactionModel getByGroupId(String groupId) {
        return HAE_TRANSACTION_MAP.get(groupId);
    }

    public static HaeTransactionModel getCurrent() {
        return current.get();
    }

    public static void setCurrentGroupId(String groupId) {
        currentGroupId.set(groupId);
    }

    public static String getCurrentGroupId() {
        return currentGroupId.get();
    }

    public static Integer getTransactionCount() {
        return transactionCount.get();
    }

    public static void setTransactionCount(int i) {
        transactionCount.set(i);
    }

    public static Integer addTransactionCount() {
        int i = (getTransactionCount() == null ? 0 : getTransactionCount() + 1);
        transactionCount.set(i);
        return i;
    }
}
